package com.crdeng.business.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crdeng.business.entity.InStock;
import com.crdeng.business.entity.Supplier;
import com.crdeng.business.service.InStockService;
import com.crdeng.business.service.SupplierService;
import com.crdeng.business.vo.*;
import com.crdeng.handler.BusinessException;
import com.crdeng.response.Result;
import com.crdeng.response.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author crdeng
 * @since 2021-01-03
 */
@Api(value = "商品入库", tags = "商品入库接口")
@RestController
@RequestMapping("/business/inStock")
public class InStockController {

    @Autowired
    private InStockService inStockService;
    @Autowired
    private SupplierService supplierService;


    @ApiOperation(value = "获取列表", notes = "获取列表")
    @GetMapping("/list")
    public Result list() {
        List<InStock> inStockList = inStockService.list();
        return Result.ok().data("list", inStockList);
    }


    @ApiOperation("条件查询")
    @PostMapping("/getList")
    public Result getList(@RequestParam(defaultValue = "1") Integer current,
                          @RequestParam(defaultValue = "6") Integer size,
                          @RequestBody InStockQueryMapVO queryMap) {
        Page<InStockVO> inStockVOPage = new Page<>(current, size);

        IPage<InStockVO> inStockPage = inStockService.findInStockPage(inStockVOPage, queryMap);

        return Result.ok().data("list", inStockPage.getRecords()).data("total", inStockPage.getTotal());
    }


    @ApiOperation(value = "明细", notes = "查询明细")
    @GetMapping("/getDetail")
    public Result getDetail(@RequestParam Long id,
                            @RequestParam(defaultValue = "1") Integer current,
                            @RequestParam(defaultValue = "3") Integer size) {

        InStockDetailVO inStockDetailVO = inStockService.getDetail(id, current, size);

        return Result.ok().data("inStockDetailVO", inStockDetailVO);
    }

    /*入库*/
    @ApiOperation(value = "添加入库")
    @PostMapping("/add")
    public Result add(@RequestBody AddInStockVO addInStockVO) {
        if (addInStockVO.getSupplierId() == null) {
            //说明现在添加物资来源
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(addInStockVO, supplierVO);
            if ("".equals(supplierVO.getName()) || supplierVO.getName() == null) {
                throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(), "物资提供方名不能为空");
            }
            if ("".equals(supplierVO.getEmail()) || supplierVO.getEmail() == null) {
                throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(), "邮箱不能为空");
            }
            if ("".equals(supplierVO.getContact()) || supplierVO.getContact() == null) {
                throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(), "联系人不能为空");
            }
            if ("".equals(supplierVO.getAddress()) || supplierVO.getAddress() == null) {
                throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(), "地址不能为空");
            }
            if ("".equals(supplierVO.getPhone()) || supplierVO.getPhone() == null) {
                throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(), "联系方式不能为空");
            }
            if (supplierVO.getSort() == null) {
                throw new BusinessException(ResultCode.PARAM_IS_BLANK.getCode(), "排序不能为空");
            }
            Supplier supplier = supplierService.add(supplierVO);
            addInStockVO.setSupplierId(supplier.getId());
        }

        inStockService.add(addInStockVO);

        return Result.ok();
    }

    @ApiOperation(value = "删除入库")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Long id) {
        inStockService.alterStatus(id, 1);
        return Result.ok();
    }

    @ApiOperation(value = "审批通过")
    @GetMapping ("/pass")
    public Result pass(@RequestParam Long id) {
        inStockService.alterStatus(id, 0);
        return Result.ok();
    }


}

